<template>
  <div class="service-contrib">
    <div class="page-header">
      <h1>服务贡献</h1>
      <div class="actions">
        <el-button type="primary" @click="openCreate">新增</el-button>
      </div>
    </div>

    <el-card>
      <el-table :data="rows" v-loading="loading">
        <el-table-column prop="workType" label="类型" width="100" />
        <el-table-column prop="category" label="工作类别" min-width="180" />
        <el-table-column prop="content" label="具体工作内容" min-width="240" show-overflow-tooltip />
        <el-table-column prop="targetOrHours" label="目标/时长" width="140" />
        <el-table-column prop="status" label="状态" width="110">
          <template #default="{ row }"><el-tag :type="statusTagType(row.status)">{{ statusText(row.status) }}</el-tag></template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button type="text" @click="edit(row)">编辑</el-button>
            <el-button type="text" style="color:#f56c6c;" @click="removeRow(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="showEdit" :title="form.id ? '编辑服务贡献' : '新增服务贡献'" width="640px">
      <el-form :model="form" label-width="100px">
        <el-form-item label="类型" required>
          <el-select v-model="form.workType" style="width:100%" @change="onTypeChange">
            <el-option label="临时性" value="临时性" />
            <el-option label="长期性" value="长期性" />
          </el-select>
        </el-form-item>
        <el-form-item label="工作类别" required>
          <el-select v-model="form.category" style="width:100%" :placeholder="form.workType==='临时性'?'请选择临时性类别':'请选择长期性类别'">
            <el-option v-for="opt in categoryOptions" :key="opt" :label="opt" :value="opt" />
          </el-select>
        </el-form-item>
        <el-form-item label="具体内容">
          <el-input v-model="form.content" type="textarea" :rows="4" />
        </el-form-item>
        <el-form-item label="目标/时长" required>
          <el-input v-model="form.targetOrHours" placeholder="例如：40小时/全年/学期制" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showEdit=false">取消</el-button>
        <el-button type="primary" :loading="saving" @click="submit">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getMyServices, createService, updateService, deleteService } from '@/api/serviceContribution'

const rows = ref([])
const loading = ref(false)
const showEdit = ref(false)
const saving = ref(false)
const form = reactive({ id: undefined, workType: '临时性', category: '', content: '', targetOrHours: '' })
const categoryOptions = ref([])

function statusText(s){ return ({ PENDING: '待审核', APPROVED: '已通过', REJECTED: '已驳回' }[s] || '待审核') }
function statusTagType(s){ return ({ PENDING: 'warning', APPROVED: 'success', REJECTED: 'danger' }[s] || 'info') }

async function load(){
  loading.value = true
  try{ const res = await getMyServices(); rows.value = res?.data || res || [] } finally { loading.value = false }
}

function openCreate(){ Object.assign(form, { id: undefined, workType: '临时性', category: '', content: '', targetOrHours: '' }); showEdit.value = true }
function edit(row){ Object.assign(form, { id: row.id, workType: row.workType, category: row.category, content: row.content, targetOrHours: row.targetOrHours }); showEdit.value = true }

async function submit(){
  if(!form.workType || !form.category){ ElMessage.warning('请填写类型与工作类别'); return }
  saving.value = true
  try{
    const payload = { work_type: form.workType, category: form.category, content: form.content, target_or_hours: form.targetOrHours }
    if(form.id) await updateService(form.id, payload)
    else await createService(payload)
    ElMessage.success('已保存')
    showEdit.value = false
    await load()
  } catch(e){ ElMessage.error(e?.response?.data?.message || e?.message || '保存失败') } finally { saving.value = false }
}

async function removeRow(row){
  await ElMessageBox.confirm('确定删除该记录？','提示',{type:'warning'})
  await deleteService(row.id)
  ElMessage.success('已删除')
  await load()
}

onMounted(load)

function onTypeChange(){
  categoryOptions.value = form.workType === '临时性'
    ? ['起草文稿', '参加学院/市教委创新大赛', '举办或参与学院/系重要活动', 'MPA教育中心工作', '转专业相关工作', '培养方案修订工作', '其他']
    : ['工会小组长', '大创管理工作', '信息员', 'MPA教育中心兼职秘书', '其他']
  // 切换类型后清空已选类别，避免类别与类型不匹配
  form.category = ''
}
// 初始化一次
onTypeChange()
</script>

<style scoped>
.service-contrib { max-width: 1200px; }
.page-header { display:flex; justify-content:space-between; align-items:center; margin-bottom:16px; }
</style>


